<template>
	<view class="container">
		<view class="map">
			<map :longitude="vdata.mapConfig.longitude" :latitude="vdata.mapConfig.latitude" style="width: 100%;height: calc(100% - 100rpx)"></map>
			<view class="address"><text>当前位置：</text>	<text>{{ vdata.mapConfig.address }}附近</text></view>
		</view>
		<view class="inner">
			<view class="group-list">
				<view class="group-item" v-for="item in vdata.sortedAgents" :key="item.agentNo">
					<view class="left">
						<image :src="'https://jdyd-marketing-public.oss-cn-hangzhou.aliyuncs.com/yx/f16e17c6-612d-4b3f-ad4f-86325fff08e3.png'"></image>
						<view class="desc">
							<view class="tle">{{ item.agentName }}</view>
							<view v-if="item.phone" class="dis1" @click="linkPhone(item.phone)">电话：
								<span class="text">{{ item.phone }}</span>
							</view>
							<view class="dis">{{ item.distance.toFixed(2) }}公里</view>
							<view class="addr">
								<image src="@/static/new/businessCircle/location.png"></image>
								<text class="text">{{ item.addr }}</text>
							</view>
						</view>
					</view>
					<view class="right">
						<image @tap="onNavigation(Number(item.jd),Number(item.wd),item.addr)" class="nav" src="@/static/new/businessCircle/nav.png"></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { onLoad } from '@dcloudio/uni-app'
	import { reactive } from 'vue'
	import getLocation from '@/util/location.js'
	import tool from '@/util/tool.js'
	// #ifdef MP-YHDD
	import agentsInfo from './agents_info_cc.json'
	// #endif
	// #ifndef MP-YHDD
	import agentsInfo from './agents_info_zz.json'
	// #endif
	
	// 数据源
	const vdata = reactive({
		mapConfig: {	// 地图相关配置
			longitude: undefined,	// 默认经度
			latitude: undefined,	// 默认纬度
			address: '',	// 默认位置
		},
		sortedAgents: [],	// 网点列表（已排序）
	})
	
	onLoad(async () => {
		const res = await getLocation()
		Object.assign(vdata.mapConfig,res)
		const sortedAgents = await sortByDistanceFromLocation(agentsInfo.agents, res.latitude, res.longitude);
		vdata.sortedAgents = sortedAgents
	})
	// 电话
	function linkPhone(phoneNumber){
		uni.makePhoneCall({
			phoneNumber
		})
	}
	/**
	 * 导航事件
	 */
	function onNavigation (longitude,latitude,address) {
		uni.openLocation({
			longitude,
			latitude,
			address
		})
	}
	
	
	/**
	 * 计算两点之间的大圆距离。使用 Haversine 公式进行计算。
	 * @param {Object} lat1
	 * @param {Object} lon1
	 * @param {Object} lat2
	 * @param {Object} lon2
	 */
	function calculateDistance(lat1, lon1, lat2, lon2) {
	    const R = 6371; // 地球平均半径，单位为公里
	    const dLat = deg2rad(lat2 - lat1);
	    const dLon = deg2rad(lon2 - lon1);
	
	    const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
	              Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
	              Math.sin(dLon / 2) * Math.sin(dLon / 2);
	    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
	    const distance = R * c;
	
	    return distance;
	}
	/**
	 * 将角度转换为弧度。
	 * @param {Object} deg
	 */
	function deg2rad(deg) {
	    return deg * (Math.PI / 180);
	}
	/**
	 * 接受一个网点列表 points 和目标经纬度 targetLatitude 和 targetLongitude。
	 * 使用 map 方法遍历每个网点，计算距离并添加到每个网点对象中。
	 * 使用 sort 方法对网点列表按照距离进行排序。
	 * 返回一个按距离从近到远排序的新数组，并且每个网点对象包含 distance 属性。
	 * @param {Object} points
	 * @param {Object} targetLatitude
	 * @param {Object} targetLongitude
	 */
	async function sortByDistanceFromLocation(points, targetLatitude, targetLongitude) {
	    // 计算每个网点到目标位置的距离，并添加到网点对象中
	    const pointsWithDistance = points.map(point => ({
	        ...point,
	        distance: calculateDistance(targetLatitude, targetLongitude, point.wd, point.jd)
	    }));
		
	
	    // 根据距离进行排序
	    return pointsWithDistance.sort((a, b) => a.distance - b.distance);
	}
</script>

<style lang="less">
	.container{
		position: relative;
		overflow: hidden;
		
		.map{
			width: 100%;
			height: 520rpx;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 2;
			background-color: #f7f8fb;
			.address{
				width: 100%;
				font-size: 36rpx;
				font-weight: bold;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				padding: 24rpx;
				box-sizing: border-box;
				color: #f7a128;
				text{
					&:nth-child(1){
						color: #000;
					}
					&:nth-child(2){
						color: #f7a128;
					}
				}
			}
		}
		
		
		.inner{
			margin-top: 520rpx;
			padding: 0 24rpx 24rpx;
			background-color: #f7f8fb;
			
			.group-list{
				display: flex;
				flex-direction: column;
				gap: 24rpx;
				margin-top: 30rpx;
				
				.group-item{
					padding: 24rpx;
					background-color: #fff;
					border-radius: 24rpx;
					display: flex;
					
					.left{
						display: flex;
						image{
							width: 146rpx;
							height: 146rpx;
							margin-right: 18rpx;
							flex-shrink: 0;
							border-radius: 15rpx;
						}
						.desc{
							width: 390rpx;
							display: flex;
							flex-direction: column;
							gap: 25rpx;
							.tle{
								font-weight: 500;
								font-size: 32rpx;
								color: #333333;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
							}
							.dis{
								font-weight: 400;
								font-size: 24rpx;
								color: #999999;
							}
							.dis1{
								font-weight: 400;
								font-size: 24rpx;
								color: #999999;
								.text{
									color:#24C789;
								}
							}
							.addr{
								display: flex;
								align-items: center;
								image{
									width: 24rpx;
									height: 26rpx;
									margin-right: 10rpx;
								}
								.text{
									white-space: nowrap;
									overflow: hidden;
									text-overflow: ellipsis;
									font-weight: 400;
									font-size: 24rpx;
									color: #999999;
								}
							}
						}
					}
					.right {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						align-items: flex-end;
						margin-left: 24rpx;
						flex-shrink: 0;
						
						.top-tag{
							background: #080808;
							color: #e3b087;
							padding: 5rpx 10rpx;
							border-radius: 8rpx;
							font-size: 24rpx;
						}
						
						.nav{
							width: 60rpx;
							height: 60rpx;
						}
					}
				}
			}
		}
	}
</style>